from DataPrepare.dailyFactorsWithInfluxdb.factorBase import factorBase
import cx_Oracle as oracle
import pandas as pd
from Config.myConfig import *
from Config.myConstant import *
from DataAccess.TradedayDataProcess import TradedayDataProcess

########################################################################
class AShareEODDerivativeIndicator(factorBase):
    """记录A股收益数据等"""
    #----------------------------------------------------------------------
    def __init__(self):
        #super(buySellVolumeRatio,self).__init__()
        super().__init__()
        self.factor='dailyAShareEODDerivativeIndicator'
        pass
    #----------------------------------------------------------------------
    def getDataFromOracleByCode(self,code,startDate,endDate):
        data=self.__getDailyDataByDateFromOracleServer(code,startDate,endDate);
        return data
        pass
    #----------------------------------------------------------------------
    #输入code=600000.SH，startdate=yyyyMMdd，endDate=yyyyMMdd
    def __getDailyDataByDateFromOracleServer(self,code,startDate=EMPTY_STRING,endDate=EMPTY_STRING):
        #获取行情数据
       
        database='wind_filesync.AShareEODDerivativeIndicator'
        connection = oracle.connect(self.oracleConnectStr)
        cursor = connection.cursor()
        oracleStr="select S_INFO_WINDCODE as code,TRADE_DT as \"date\",S_VAL_PE,S_VAL_PB_NEW,S_VAL_PE_TTM,S_VAL_PCF_OCF,S_VAL_PCF_OCFTTM,     S_VAL_PCF_NCF,S_VAL_PCF_NCFTTM,S_VAL_PS,S_VAL_PS_TTM,S_PRICE_DIV_DPS,NET_PROFIT_PARENT_COMP_TTM,NET_PROFIT_PARENT_COMP_LYR,             NET_ASSETS_TODAY,NET_CASH_FLOWS_OPER_ACT_TTM,NET_CASH_FLOWS_OPER_ACT_LYR,OPER_REV_TTM,OPER_REV_LYR,UP_DOWN_LIMIT_STATUS FROM {0} ".format(database)
        if startDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' order by TRADE_DT".format(code)
        elif endDate==EMPTY_STRING:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} order by TRADE_DT".format(code,startDate)
        else:
            oracleStr=oracleStr+"where S_INFO_WINDCODE='{0}' and TRADE_DT>={1} and TRADE_DT<={2} order by TRADE_DT".format(code,startDate,endDate)
        cursor.execute(oracleStr)


        mydata = cursor.fetchall()
        mydata = pd.DataFrame(mydata,columns=['code','date','S_VAL_PE','S_VAL_PB_NEW','S_VAL_PE_TTM','S_VAL_PCF_OCF','S_VAL_PCF_OCFTTM',     'S_VAL_PCF_NCF','S_VAL_PCF_NCFTTM','S_VAL_PS','S_VAL_PS_TTM','S_PRICE_DIV_DPS','NET_PROFIT_PARENT_COMP_TTM','NET_PROFIT_PARENT_COMP_LYR','NET_ASSETS_TODAY','NET_CASH_FLOWS_OPER_ACT_TTM','NET_CASH_FLOWS_OPER_ACT_LYR','OPER_REV_TTM','OPER_REV_LYR','UP_DOWN_LIMIT_STATUS'])
        mydata[['S_VAL_PE','S_VAL_PB_NEW','S_VAL_PE_TTM','S_VAL_PCF_OCF','S_VAL_PCF_OCFTTM',     'S_VAL_PCF_NCF','S_VAL_PCF_NCFTTM','S_VAL_PS','S_VAL_PS_TTM','S_PRICE_DIV_DPS','NET_PROFIT_PARENT_COMP_TTM','NET_PROFIT_PARENT_COMP_LYR','NET_ASSETS_TODAY','NET_CASH_FLOWS_OPER_ACT_TTM','NET_CASH_FLOWS_OPER_ACT_LYR','OPER_REV_TTM','OPER_REV_LYR','UP_DOWN_LIMIT_STATUS']] = mydata[['S_VAL_PE','S_VAL_PB_NEW','S_VAL_PE_TTM','S_VAL_PCF_OCF','S_VAL_PCF_OCFTTM',     'S_VAL_PCF_NCF','S_VAL_PCF_NCFTTM','S_VAL_PS','S_VAL_PS_TTM','S_PRICE_DIV_DPS','NET_PROFIT_PARENT_COMP_TTM','NET_PROFIT_PARENT_COMP_LYR','NET_ASSETS_TODAY','NET_CASH_FLOWS_OPER_ACT_TTM','NET_CASH_FLOWS_OPER_ACT_LYR','OPER_REV_TTM','OPER_REV_LYR','UP_DOWN_LIMIT_STATUS']].astype('float')
        mydata['mytime']=pd.to_datetime(mydata['date'],format='%Y%m%d')
        mydata.set_index('mytime',inplace=True,drop=True)
        return mydata  
########################################################################
